package com.tarena.lbs.user.dao.repository.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tarena.lbs.base.protocol.pager.BasePageQuery;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.user.dao.mapper.UserMapper;
import com.tarena.lbs.user.dao.repository.UserRepository;
import com.tarena.lbs.user.pojo.param.UserLoginParam;
import com.tarena.lbs.user.pojo.param.UserTagsParam;
import com.tarena.lbs.user.pojo.po.UserGroupPO;
import com.tarena.lbs.user.pojo.po.UserPO;
import com.tarena.lbs.user.pojo.po.UserTagsPO;
import com.tarena.lbs.user.pojo.query.UserQuery;
import com.tarena.lbs.user.pojo.vo.UserGroupVO;
import com.tarena.lbs.user.pojo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@Repository
public class UserRepositoryImpl implements UserRepository {
    @Autowired
    UserMapper userMapper;
    @Override
    public UserPO login(UserLoginParam param) {
        UserPO userPO = userMapper.login(param);
        return userPO;
    }

    @Override
    public UserVO detail(Integer id) {
        UserVO userVO = userMapper.detail(id);
        return userVO;
    }

    @Override
    public UserVO selectByPhone(String phone) {
        UserVO userVO = userMapper.selectByPhone(phone);
        return userVO;
    }

    @Override
    public void save(UserPO userPO) {
        userMapper.insert(userPO);
    }

    @Override
    public PageResult<UserVO> selectPageList(UserQuery userQuery) {
        // 1. 构建分页查询
        Page<UserPO> page = new Page<>(userQuery.getPageNo(), userQuery.getPageSize());
        LambdaQueryWrapper<UserPO> wrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNotBlank(userQuery.getUserName())) {
            wrapper.like(UserPO::getUserName, userQuery.getUserName());
        }

        if (StringUtils.isNotBlank(userQuery.getPhone())) {
            wrapper.eq(UserPO::getPhone, userQuery.getPhone());
        }

        if (userQuery.getProviderType() != null) {
            wrapper.eq(UserPO::getProviderType, userQuery.getProviderType());
        }

        // 时间范围查询
        if (userQuery.getStartingTime() != null || userQuery.getEndTime() != null) {
            if (userQuery.getStartingTime() != null && userQuery.getEndTime() != null) {
                wrapper.between(UserPO::getRegTime, userQuery.getStartingTime(), userQuery.getEndTime());
            } else if (userQuery.getStartingTime() != null) {
                wrapper.ge(UserPO::getRegTime, userQuery.getStartingTime());
            } else {
                wrapper.le(UserPO::getRegTime, userQuery.getEndTime());
            }
        }

        // 2. 执行分页查询
        IPage<UserPO> poPage = userMapper.selectPage(page, wrapper);
        log.debug("查询结果：{}", poPage); // 使用 debug 级别日志

        // 3. 转换为 VO
        List<UserVO> voList = poPage.getRecords().stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());

        // 4. 创建分页结果
        PageResult<UserVO> pageResult = new PageResult<>(
                new BasePageQuery(userQuery.getPageNo(), userQuery.getPageSize())
        );
        pageResult.setObjects(voList);
        pageResult.setTotal(poPage.getTotal());

        return pageResult;
    }

    public UserVO convertToVO(UserPO po) {
        UserVO userVO = new UserVO();
        userVO.setId(po.getId());
        userVO.setUserName(po.getUserName());
        userVO.setPassword(po.getPassword());
        userVO.setNickName(po.getNickName());
        userVO.setIpAddress(po.getIpAddress());
        userVO.setRegTime(po.getRegTime());
        userVO.setLastLogTime(po.getLastLogTime());
        userVO.setIntroduction(po.getIntroduction());
        userVO.setPhone(po.getPhone());
        userVO.setProviderType(po.getProviderType());
        userVO.setStatus(po.getStatus());
        userVO.setUnionId(po.getUnionId());
        userVO.setUserNickPicture(po.getUserImage());
        return userVO;
    }


}
